package com.md.application.assembler;

import com.md.base.exception.ServerException;
import com.md.domain.model.bo.QueryModuleBO;
import com.md.domain.model.po.ModulePO;
import com.md.infrastructure.value.ErrorEnum;
import com.md.request.ModuleRequest;
import com.md.request.QueryModuleRequest;
import com.md.response.ModuleResponse;
import com.md.util.ClassUtils;
import com.md.util.ObjectUtil;
import com.md.value.ModuleTypeEnum;
import lombok.experimental.UtilityClass;

import java.util.Optional;

/**
 * @author 刘骄阳
 * 2022-10-17 15:01
 */
@UtilityClass
public class ModuleAssembler {

  public static void checkParamValue(final Long id, final String appCode, final Long tenantId, final Long brandId) {
    if (ObjectUtil.isAllEmpty(id, appCode, tenantId, brandId)) {
      throw ServerException.of(ErrorEnum.PARAM_IS_ALL_EMPTY);
    }
  }

  public static ModulePO buildModulePO(final ModuleRequest request) {
    return ClassUtils.source2Target(request, ModulePO.class, modulePO -> {
      Optional.ofNullable(request.getModuleType()).map(ModuleTypeEnum::getCode).ifPresent(modulePO::setModuleType);
    });
  }

  public static ModulePO buildModulePO(final QueryModuleRequest request) {
    return ClassUtils.source2Target(request, ModulePO.class);
  }

  public static QueryModuleBO buildQueryModuleBO(final QueryModuleRequest request) {
    return ClassUtils.source2Target(request, QueryModuleBO.class, queryModuleBO -> {
      Optional.ofNullable(request).map(QueryModuleRequest::getModuleType).map(ModuleTypeEnum::getCode)
        .ifPresent(queryModuleBO::setModuleType);
    });
  }

  public static ModulePO buildModulePO(final QueryModuleBO queryBO) {
    return ClassUtils.source2Target(queryBO, ModulePO.class);
  }

  public static ModuleResponse buildModulePO2Response(final ModulePO modulePO) {
    return ClassUtils.source2Target(modulePO, ModuleResponse.class, moduleResponse -> {
      moduleResponse.setModuleType(ModuleTypeEnum.of(modulePO.getModuleType()));
    });
  }

}
